﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Oliver.Controls
{
    public class BackgroundStar : UserControl
    {

        public BackgroundStar()
        {
            this.Loaded += new RoutedEventHandler(BackgroundStar_Loaded);
        }

        void BackgroundStar_Loaded(object sender, RoutedEventArgs e)
        {
            DrawContent(this.ActualWidth, this.ActualHeight);
        }

        //private double _radius = 100;
        //public double Radius
        //{
        //    get { return _radius; }
        //    set
        //    {
        //        if (value < 1) value = 1;
        //        _radius = value; 
        //        InvalidateMeasure();
        //    }
        //}

        private int _triangles = 10;
        public int Triangles
        {
            get { return _triangles; }
            set
            {
                if (value < 1) value=1;
                _triangles = value; 
                InvalidateMeasure(); }
        }

        private Brush _color1 = new SolidColorBrush(Colors.Yellow);
        public Brush Color1
        {
            get { return _color1; }
            set { _color1 = value; InvalidateMeasure();  }
        }

        private Brush _color2 = new SolidColorBrush(Colors.Orange);
        public Brush Color2
        {
            get { return _color2; }
            set { _color2 = value; InvalidateMeasure(); }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            DrawContent(availableSize.Width, availableSize.Height);
            return base.MeasureOverride(availableSize);
        }

        private void DrawContent(double width, double height)
        {
            double radius = width > height ? height : width;
            
            radius /= 2;

            Canvas canvas = new Canvas();

            double alphastep = Convert.ToDouble(360) / Convert.ToDouble(Triangles);
            
            double x = width / 2;
            double y = height / 2;

            for (int i = 1; i <= Triangles; i++)
            {
                bool alternating = i % 2 == 0;
                double alpha = alphastep * i;

                Path p = new Path();
                p.Fill = alternating ? Color1 : Color2;

                PathGeometry pg = new PathGeometry();
                p.Data = pg;

                PathFigure pf = new PathFigure();
                //pf.StartPoint = new Point(0, 0);

                PathFigureCollection pfc = new PathFigureCollection();
                pfc.Add(pf);
                
                LineSegment ls1 = new LineSegment();
                Point point1 = new Point();
                point1.X = radius * Math.Cos(alpha);
                point1.Y = radius * Math.Sin(alpha);
                ls1.Point = point1;
                pf.Segments.Add(ls1);

                LineSegment ls2 = new LineSegment();
                Point point2 = new Point();
                point2.X = radius * Math.Cos(alpha + alphastep);
                point2.Y = radius * Math.Sin(alpha + alphastep);
                ls2.Point = point2;
                pf.Segments.Add(ls2);
                
                //LineSegment ls3 = new LineSegment();
                //ls3.Point = new Point(0,0);
                //pf.Segments.Add(ls3);
                
                p.SetValue(Canvas.LeftProperty, x);
                p.SetValue(Canvas.TopProperty, y);

                pg.Figures = pfc;

                canvas.Children.Add(p);
            }

            this.Content = canvas;
        }
    }
}
